package com.cheetah.start.common.plugin.log;

import com.cheetah.start.common.utils.IpUtil;
import com.cheetah.start.common.utils.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 控制台日志打印
 *
 * @author xjh
 * @date 2023/03/18
 */
@Slf4j
@Component
public class ConsoleLog {

    @Autowired
    private Ip2regionSearcher regionSearcher;

    /**
     * 请求出现错误日志
     */
    public void logErrorRequest() {
        HttpServletRequest request = ServletUtil.getRequest();
        String param = ServletUtil.getParam(request);
        String browseInfo = ServletUtil.getBrowseInfo(request);
        String ipAddress = IpUtil.getIpNotHavePort(request);
        String address = regionSearcher.getAddress(ipAddress);
        log.error("\n┏━━━━ 异常地址:[{}],\n┣ 请求方式:[{}],\n┣ 参数:{}\n┣ IP地址:[{}]\n┣ 其他信息:[{}]",
                request.getRequestURI(),
                request.getMethod() + "|" + request.getContentType(),
                param,
                ipAddress + "|" + address,
                browseInfo
        );
    }
}
